<template>
    <div>
      <el-button @click="logout" type="danger">注销</el-button>
      <el-button @click="created" type="primary">刷新</el-button>
      &nbsp;
      <!-- 跳转到上传文件 -->
        <router-link to="/upload">
            <el-button type="primary">上传文件</el-button>
        </router-link>

      <el-table :data="files" style="width: 100%">
        <el-table-column prop="appId" label="App ID"></el-table-column>
        <el-table-column prop="createdTime" label="创建时间"></el-table-column>
        <el-table-column prop="fileName" label="文件名称"></el-table-column>
        <el-table-column prop="ReportGenerated" label="是否修改">
            <template slot-scope="scope">
                <el-tag v-if="scope.row.reportGenerated===1" type="success">已修改</el-tag>
                <el-tag v-else type="danger">未修改</el-tag>
            </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="updateFileStatus(scope.row.appId, scope.row.fileName)" type="primary">修改状态</el-button>
            <el-button @click="getFileUrl(scope.row.appId, scope.row.fileName)" type="success">获取文件URL</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </template>
  
  <script>
  export default {
    data() {
      return {
        files: []
      };
    },
    created() {
      this.created();
    },

    methods: {
        async created() {
      try {
        const response = await this.axios.get(this.$https+'/api/Report/all');
        this.files = response.data.data;
      } catch (error) {
        this.$message.error('获取文件列表失败');
      }
    },
    async updateFileStatus(appId, fileName) {
      try {
        const formData = new FormData();
        formData.append('appId', appId);
        formData.append('fileName', fileName);
        await this.axios.post(this.$https+'/api/Report/update', formData);
        this.$message.success('文件状态更新成功');
        this.created();
      } catch (error) {
        this.$message.error('文件状态更新失败');
      }
    }
      ,
      async getFileUrl(appId, fileName) {
        try {
          const response =  this.axios.get(this.$https+'/api/Report/file', {
            params: { appId, fileName }
          });
          //跳转
            window.open(response.data);
        } catch (error) {
          this.$message.error('获取文件URL失败');
        }
      },
      logout() {
        localStorage.removeItem('user');
        this.$router.push('/');
      }
    }
    };
  </script>